package com.hongdee.market.bc.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.market.bc.entity.TBcCustomeLevelConf;
import com.hongdee.market.bc.service.ITBcCustomLevelConfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/bc/custome/level/conf")
/**
 * Created by dell on 2017/5/15.
 */
public class TBcCustomeLevelConfController {
    @Autowired
    private ITBcCustomLevelConfService itBcCustomLevelConfService;

    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TBcCustomeLevelConf tBcCustomeLevelConf) throws Exception {
        EntityUtils.valid(tBcCustomeLevelConf);
        tBcCustomeLevelConf.setCompanyId(TokenUtil.getCompanyId(uid));
        TBcCustomeLevelConf tc = itBcCustomLevelConfService.saveOrUpdate(tBcCustomeLevelConf);
        ResultEntity res = new ResultEntity();
        res.setData(tc);
        res.setSuccess(true);
        res.setMsg("保存成功！");
        return res;
    }

    /*
    * 查询
    * */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        Map map = itBcCustomLevelConfService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    /**
     * 停用
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itBcCustomLevelConfService.update(id, "0");
        res.setSuccess(true);
        res.setMsg("停用成功！");
        return res;
    }

    /**
     * 停用
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        ResultEntity res = new ResultEntity();
        itBcCustomLevelConfService.update(id, "1");
        res.setSuccess(true);
        res.setMsg("启动成功！");
        return res;
    }

    /*
    * 分页
    * */
    @RequestMapping("/page")
    @ResponseBody
    public Map newPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                       @RequestParam(name = "token", defaultValue = "") String token,
                       @RequestParam(name = "start", defaultValue = "0") String start,
                       @RequestParam(name = "limit", defaultValue = "50") String limit,
                       @RequestParam(name = "levelCode", defaultValue = "") String levelCode
    ) throws Exception {
        String comId = TokenUtil.getCompanyId(uid);
        Map map = this.itBcCustomLevelConfService.newPage(comId, start, limit, levelCode);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        System.out.print("1");
        return map;
    }

    /***
     * 车辆等级报表 - 等级完成情况-目标值
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/target")
    @ResponseBody
    public ResultEntity target(@RequestParam(name = "uid", defaultValue = "") String uid,
                               @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {
        String comId = TokenUtil.getCompanyId(uid);

        List ls = this.itBcCustomLevelConfService.target(comId);
        ResultEntity res = new ResultEntity();
        res.setDataList(ls);
        res.setSuccess(true);
        res.setMsg("查询成功");
        return res;
    }

    /**
     * 根据等级编码查询详情
     * @param uid
     * @param token
     * @param code
     * @return
     * @throws Exception
     */
    @RequestMapping("/code")
    @ResponseBody
    public ResultEntity code(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "code", defaultValue = "") String code) throws Exception {
        String companyId = TokenUtil.getCompanyId(uid);
        ResultEntity res = new ResultEntity();
        Map map = itBcCustomLevelConfService.code(code,companyId);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }
}
